package com.bajiao.service.account.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bajiao.common.util.FastJsonHelper;
import com.bajiao.vo.account.AccountBillAdmin;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bajiao.entity.account.Account;
import com.bajiao.entity.account.AccountBill;
import com.bajiao.mapper.account.AccountBillMapper;
import com.bajiao.service.account.AccountBillService;
import com.bajiao.service.account.AccountService;
import com.bajiao.vo.account.AccountBillVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 账单
 * @Author: FanJq
 * @Date:   2024-10-1005-11
 * @Version: V1.0
 */
@Service
public class AccountBillServiceImpl extends ServiceImpl<AccountBillMapper, AccountBill> implements AccountBillService {

    /**
     * @Description
     * 创建资金流水
     * @date 2024/12/12 10:15
     * @author FanJq
     * @param userId
     * @param changeMoney 变化金额
     * @param billType 1:收入 2：支出
     * @param type 流水类型 1:充值 2:购买优惠券 3:购买商品 4:优惠券退款 5:商品退款 6:注册赠送 7:邀请奖励 99:平台赠送
     * @param orderNo 订单编号
     * @param title 标题
     * @return {@link Boolean}
    */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Boolean crateBill(String userId, BigDecimal changeMoney, Integer billType, int type, String orderNo, String title, Account account) {
        if (ObjectUtil.isNull(account))
            return true;
        BigDecimal beforeMoney = account.getMoney();
        AccountBill accountBill = new AccountBill();
        //记录账单流水
        accountBill.setTitle(title);
        accountBill.setBeforeMoney(beforeMoney);
        accountBill.setChangeMoney(changeMoney);
        accountBill.setAfterMoney(billType == 1 ? beforeMoney.add(changeMoney) : beforeMoney.subtract(changeMoney));
        accountBill.setBillType(billType);
        accountBill.setOrderNo(orderNo);
        accountBill.setUserId(userId);
        accountBill.setType(type);
        accountBill.setSign(billType == 1 ? "+" : "-");
        accountBill.setCreateTime(new Date());
        return this.save(accountBill);
    }

    /**
     * @Description
     * 管理后台列表
     * @date 2025/1/2 15:46
     * @author FanJq
     * @param params
     * @return {@link List< AccountBillAdmin>}
    */
    @Override
    public List<AccountBillAdmin> getAdminList(Map<String, Object> params) {
        return baseMapper.getAdminList(params);
    }

    /**
     * @Description
     * 账单明细
     * @date 2024/12/25 14:31
     * @author FanJq
     * @param userId
     * @param filterDate
     * @return {@link List< AccountBillVO>}
    */
    @Override
    public List<AccountBillVO> getBillPage(String userId, String filterDate) {
        List<AccountBill> billList = super.lambdaQuery()
                .eq(AccountBill::getUserId, userId)
                .apply(StrUtil.isNotBlank(filterDate), "date_format(create_time, '%Y-%m-%d') = {0}", filterDate)
                .orderByDesc(AccountBill::getCreateTime)
                .list();
        List<AccountBillVO> responseList = billList.stream().map(bill -> {
            AccountBillVO response = FastJsonHelper.beanToBean(bill, AccountBillVO.class);
            response.setChangeMoneyStr(response.getChangeMoney().stripTrailingZeros().toPlainString());
            return response;
        }).collect(Collectors.toList());
        return responseList;
    }
}
